package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.dao.IEumRoleServiceDao;
import com.newsmanagement.dao.IResourceRoleServiceDao;
import com.newsmanagement.dao.IRoleServiceDao;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IRoleService;
import com.newsmanagement.util.CheckAndChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 角色管理service实现
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {


    @Autowired
    private IRoleServiceDao roleServiceDao;

    @Autowired
    private IResourceRoleServiceDao resourceRoleServiceDao;

    @Autowired
    private IEumRoleServiceDao eumRoleServiceDao;


    /**
     * 添加角色信息
     * @param userId 创建人id
     * @param roleName 角色名称
     * @return
     */
    @Override
    public ReturnedMessages addRole(Integer userId, String roleName){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            userId = Constants.INT_ONE;//最高管理员
        }

        //验证角色名称
        if(!CheckAndChangeUtil.checkStringHaveData(roleName)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑角色名称");
        }
        roleName = roleName.trim();

        //根据角色名称验证是否已经存在相应的角色
        //如果不存在，添加
        //如果存在已删除则修改
        //如果存在未删除，则不予理会
        Map<String ,Object> roleTest = roleServiceDao.queryRoleByRoleNametest(roleName);

        if (null != roleTest && Integer.valueOf(roleTest.get("DELETE_FLAG") + "") == Constants.INT_ONE){
            //存在未删除，不予处理

            return returnedMessages.ReturnedMessagesFail(500 ,"角色已存在，不予添加");
        }else if (null != roleTest && Integer.valueOf(roleTest.get("DELETE_FLAG") + "") != Constants.INT_ONE){
            //存在已删除，修改

            //激活角色表信息
            roleServiceDao.activationRoleByRoleName(userId ,roleName);
            return returnedMessages;
        }

        //不存在添加
        int addNumber = roleServiceDao.saveRole(userId ,roleName);

        if (CheckAndChangeUtil.checkIntMoreThanZero(addNumber)){
            returnedMessages.setData(addNumber);
            return returnedMessages;
        }else{
            return returnedMessages.ReturnedMessagesFail(400 ,"数据插入数据库时出错");
        }
    }


    /**
     * 修改角色
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @param roleName 角色名称
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages updateRole(Integer userId, Integer roleId, String roleName){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            userId = Constants.INT_ONE;//最高管理员
        }

        //验证角色id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(roleId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"数据传递错误：未查找到角色id");
        }

        //验证角色名称
        if(!CheckAndChangeUtil.checkStringHaveData(roleName)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑角色名称");
        }
        roleName = roleName.trim();

        //验证想要修改成的名称是否已经被占用
        //如果已经被使用则名称不可用
        Map<String ,Object> role = roleServiceDao.queryRoleByRoleName(roleName);
        if (null != role){
            return returnedMessages.ReturnedMessagesFail(500 ,"角色名称已被占用，请编辑角色名称");
        }

        //修改
        roleServiceDao.updateRoleById(userId ,roleId ,roleName);
        return returnedMessages;

//        int updateNumber = roleServiceDao.updateRoleById(userId ,roleId ,roleName);
//
//        if (CheckAndChangeUtil.checkIntMoreThanZero(updateNumber)){
//            returnedMessages.setData(updateNumber);
//            return returnedMessages;
//        }else{
//            return returnedMessages.ReturnedMessagesFail(400 ,"数据库修改数据时出错");
//        }
    }

    /**
     * 删除角色
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages deleteRole(Integer userId, Integer roleId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            userId = Constants.INT_ONE;//最高管理员
        }

        //验证角色id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(roleId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"数据传递错误：未查找到角色id");
        }

        //删除
        roleServiceDao.deleteRole(userId ,roleId);
        //根据角色id，删除对应的可请求资源
        resourceRoleServiceDao.deleteResourceRoleByRoleId(userId ,roleId);
        //根据角色id，删除对应的用户角色关系表信息
        eumRoleServiceDao.deleteEumRoleByRoleId(userId ,roleId);

        return returnedMessages;
    }

    /**
     * 设置角色对应可请求资源
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @param resourceIds 资源id集：使用“，”隔开
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages addResource(Integer userId ,Integer roleId ,String resourceIds){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            userId = Constants.INT_ONE;//最高管理员
        }

        //验证角色id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(roleId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"数据传递错误：未查找到角色id");
        }

        //验证资源ids resourceIds
        String[] resourceArray = CheckAndChangeUtil.getStringArrayIsDigital(resourceIds);
        if(null == resourceArray){
            return returnedMessages.ReturnedMessagesFail(500 ,"数据传递错误：未查到选择的资源id集");
        }

        //添加关系表数据
        for (int i = 0; i < resourceArray.length; i++) {
            resourceRoleServiceDao.addResourceRole(userId ,roleId ,Integer.valueOf(resourceArray[i].trim()));
        }
        return returnedMessages;
    }

}
